<template>
  <div class="login-container">
    <div class="login-box">
      <h2>登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label>邮箱：</label>
          <input 
            type="email" 
            v-model="email" 
            required 
            placeholder="请输入邮箱"
          >
        </div>
        <div class="form-group">
          <label>密码：</label>
          <input 
            type="password" 
            v-model="password" 
            required 
            placeholder="请输入密码"
          >
        </div>
        <button type="submit">登录</button>
        <div class="links">
          <router-link to="/register">还没有账号？立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/userStore'

const router = useRouter()
const userStore = useUserStore()
const email = ref('')
const password = ref('')

// TODO: API - 用户登录
const handleLogin = async () => {
  try {
    // const response = await request.post('/api/login', {
    //   email: email.value,
    //   password: password.value
    // })
    
    // 模拟登录成功
    const mockResponse = {
      token: 'mock-token-123',
      user: {
        id: 1,
        email: email.value,
        name: '测试用户'
      }
    }
    
    // 使用 store 保存登录状态
    userStore.setLoginState(mockResponse.token, mockResponse.user)
    
    router.push('/home')
  } catch (error) {
    alert('登录失败：邮箱或密码错误')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.form-group {
  margin-bottom: 15px;
}

label {
  display: block;
  margin-bottom: 5px;
}

input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

.links {
  margin-top: 15px;
  text-align: center;
}

a {
  color: #4CAF50;
  text-decoration: none;
}
</style>

